import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.frame.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;

public class ROI_analyser_ extends PlugInFrame implements ActionListener 
{
	int rcounter=1;
	JButton BselectROI,BregisterROI,Bcopy,Bclear;
	JLabel LdataROI;
	JTextField TAdose;
	boolean bRectROI=false;
	int xROI=1,yROI=1,wROI=10,hROI=10;
	JTable Ta;
	
	void update_LdataROI()
	{	
		String dataROI=(bRectROI?"RECT":"CIRC")+" ("+xROI+","+yROI+")-("+(xROI+wROI)+","+(yROI+hROI)+")";
		LdataROI.setText(dataROI);
	}
	
	public ROI_analyser_() 
	{	
		super("ROI analyser");setBackground(SystemColor.control);setLayout(new BorderLayout());
		JPanel JP1=new JPanel();JP1.setLayout(new GridLayout(2,2));
		BregisterROI=new JButton("Register ROI");BregisterROI.setToolTipText("register ROI position to be used for measurement (rectangular and circular ROI's allowed)");
		BregisterROI.addActionListener(this);JP1.add(BregisterROI);LdataROI=new JLabel(""); 
		JP1.add(LdataROI);
		BselectROI=new JButton("Measure ROI");BselectROI.setToolTipText("measures the registered ROI of the active Image");
		BselectROI.addActionListener(this);JP1.add(BselectROI);
		TAdose=new JTextField("");TAdose.setToolTipText("fill in a parameter for the active image");JP1.add(TAdose);		
		add(JP1,BorderLayout.NORTH);
		Ta=new JTable(15,8);Ta.setEnabled(false);
		Ta.setPreferredScrollableViewportSize(new Dimension(100,100));
		JScrollPane sp = new JScrollPane(Ta);add(sp,BorderLayout.CENTER);
		add(sp,BorderLayout.CENTER);
		Ta.setValueAt("image name",0,0);
		Ta.setValueAt("dose parameter",0,1);
		Ta.setValueAt("mean",0,2);
		Ta.setValueAt("stdev",0,3);
		Ta.setValueAt("kVp",0,4);
		Ta.setValueAt("mAs",0,5);
		Ta.setValueAt("thickness",0,6);
		Ta.setValueAt("det temp",0,7);
		JPanel JP2=new JPanel();JP2.setLayout(new GridLayout(1,2));
		Bcopy=new JButton("Copy data");Bcopy.setToolTipText("copy all data to system clipboard");
		Bcopy.addActionListener(this);JP2.add(Bcopy);
		Bclear=new JButton("Clear data");
		Bclear.addActionListener(this);JP2.add(Bclear);
		add(JP2,BorderLayout.SOUTH);
		pack();show();
		update_LdataROI();
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		String str,partstr;
		int i,j;
		if (e.getSource() == Bcopy)
		{
			JTextArea TAcp=new JTextArea();
			for(j=0;j<Ta.getRowCount();j++)
			{
				for(i=0;i<Ta.getColumnCount();i++)
					if(Ta.getValueAt(j,i)==null)
						TAcp.append("\t");
					else
						TAcp.append((String)Ta.getValueAt(j,i)+"\t");
				TAcp.append("\n");
			}
			TAcp.selectAll();
			TAcp.copy();
			return;
		} 
		
		if (e.getSource() == Bclear)
		{
			for(j=0;j<Ta.getRowCount();j++)
			{
				for(i=0;i<Ta.getColumnCount();i++)
					Ta.setValueAt("",j,i);
			}
			rcounter=1;
			Ta.setValueAt("image name",0,0);
			Ta.setValueAt("dose parameter",0,1);
			Ta.setValueAt("mean",0,2);
			Ta.setValueAt("stdev",0,3);
			Ta.setValueAt("kVp",0,4);
			Ta.setValueAt("mAs",0,5);
			Ta.setValueAt("thickness",0,6);
			Ta.setValueAt("det temp",0,7);
			return;
		} 
		
		ImagePlus imp = WindowManager.getCurrentImage();
		if (imp == null) 
		{
			IJ.error("There is no active image! Please open an image.");
			return;
		}

		ImageProcessor ip=imp.getProcessor();
		
		if (e.getSource() == BregisterROI)
		{
			Roi activeROI=imp.getRoi();
			if (activeROI==null)
				return;
			if (activeROI.getType()==Roi.OVAL)
				bRectROI=false;
			else
				bRectROI=true;			
			Rectangle r=activeROI.getBoundingRect();
			xROI=r.x;yROI=r.y;wROI=r.width;hROI=r.height;	
			update_LdataROI();
		}
		
		if (e.getSource() == BselectROI)
		{
			if (bRectROI==true)
			{	
				IJ.makeRectangle(xROI,yROI,wROI,hROI);
			}
			else
			{	
				IJ.makeOval(xROI,yROI,wROI,hROI);
			}
			ImageStatistics imstat=new ImageStatistics();
			imstat=imp.getStatistics();
			Ta.setValueAt(imp.getTitle(),rcounter,0);
			Ta.setValueAt(TAdose.getText(),rcounter,1);
			Ta.setValueAt(""+imstat.mean,rcounter,2);
			Ta.setValueAt(""+imstat.stdDev,rcounter,3);
			str=(String)imp.getProperty("Info")+"\n";
			i=str.indexOf("0018,0060");
			if (i>0)
			{
				i=str.indexOf(":",i)+1;
				j=str.indexOf((char)10,i);
				partstr=str.substring(i,j);
				
			}
			else 
				partstr="-";
			Ta.setValueAt(partstr,rcounter,4);
			i=str.indexOf("0018,1152");
			if (i>0)
			{
				i=str.indexOf(":",i)+1;
				j=str.indexOf((char)10,i);
				partstr=str.substring(i,j);
				
			}
			else 
				partstr="-";
			Ta.setValueAt(partstr,rcounter,5);
			
			i=str.indexOf("0018,11A0");
			if (i>0)
			{
				i=str.indexOf(":",i)+1;
				j=str.indexOf((char)10,i);
				partstr=str.substring(i,j);
				
			}
			else 
				partstr="-";
			Ta.setValueAt(partstr,rcounter,6);
			
			i=str.indexOf("0018,7001");
			if (i>0)
			{
				i=str.indexOf(":",i)+1;
				j=str.indexOf((char)10,i);
				partstr=str.substring(i,j);
				
			}
			else 
				partstr="-";
			Ta.setValueAt(partstr,rcounter,7);
			
			
			rcounter++;
			if (rcounter==Ta.getRowCount())
				rcounter=1;
		} 
		
		return;
	}
}
